package main

import (
	"bytes"
	"fmt"
	"go-web-battle/cp6/util"
	"io"
	"net"
	"os"
)

func main() {
	//建立tcp连接
	conn, err := net.Dial("tcp", "127.0.0.1:8087")
	util.CheckError(err)

	_, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
	util.CheckError(err)

	//读取全部数据
	result, err := fullyRead(conn)
	util.CheckError(err)
	fmt.Println(string(result))

	os.Exit(0)
}

func fullyRead(conn net.Conn) ([]byte, error) {
	defer conn.Close()

	result := bytes.NewBuffer(nil)
	var buf [512]byte
	for {
		n, err := conn.Read(buf[0:])
		result.Write(buf[0:n])
		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, err
		}
	}
	return result.Bytes(), nil
}
